# import hashlib
from django.contrib.auth import logout
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from blog.models import User
import hashlib
from django.dispatch import Signal

# def login_page(request):
#     return render(request, 'login.html')


def login_view(request):
    # return render(request, 'login1.html')
    if request.method == 'GET':
        # 检查登录状态
        if request.session.get('username') and request.session.get('uuid'):
            return HttpResponse('see已登录')
        elif request.COOKIES.get('username') and request.COOKIES.get('uuid'):
            request.session['username'] = request.cooKIES.get('username')
            request.session['uuid'] = request.CooKIES.get('uuid')
            return HttpResponse('已登录')
        return render(request, 'login.html')

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        try:
            user = User.objects.get(username=username)
        except Exception as e:
            return HttpResponse('用户名不存在')
        # 密码比对
        try:
            md = hashlib.md5()
            md.update(password.encode())
            md_password = md.hexdigest()
            print(username, md_password)
            if md_password != user.password:
                request.session['error'] = '密码错误'
                return HttpResponseRedirect("/blog/login/")
                #return HttpResponse('密码错误')
            user = User.objects.get(username=username)
        except Exception as e:
            print(f'密码异常{e}')
            return HttpResponse('密码异常')
        #      记录会话状态

        request.session['username'] = username
        request.session['uuid'] = user.id


        resp = HttpResponse('登录成功')
        if 'remember' in request.POST:
            resp.set_cookie('username', username, 3600 * 24 * 7)
            resp.set_cookie('uuid', user.id, 3600 * 24 * 7)

        return HttpResponseRedirect('/blog/index/')


# Create your views here:
# def login_views(request):
#     return render(request, 'login.html')


def reg_view(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    elif request.method == 'POST':

        username = request.POST['username']
        password = request.POST['password']

        md = hashlib.md5()
        md.update(password.encode())
        password = md.hexdigest()
        print(f'加密后的密码是{password}')
        old_users = User.objects.filter(username=username)
        if old_users:
            return HttpResponse('用户名已注册')
        # 3，插入数据【明文处理密码】
        # md = hashlib.md5()
        # md.update(password.encode())
        # password = md.hexdigest()


        User.objects.create(username=username, password=password)

        # return HttpResponse('注册成功')
        request.session['username'] = username
        return HttpResponseRedirect('/blog/index/')


def index(request):
    if request.session.get('username') and request.session.get('uuid'):
        return render(request, 'index.html')
    elif request.COOKIES.get('username') and request.COOKIES.get('uuid'):
        return render(request, 'index.html')
    else:
        return HttpResponseRedirect('/blog/login')


def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/blog/index')
    # return HttpResponse('退出成功')

def blog1_view(request):
    return render(request, 'blog1.html')

